using UnityEngine;

public class EngineSound : MonoBehaviour
{
	public AudioClip soundGearChange;

	public AudioClip soundEngine;

	public AudioClip OnGetIn;

	public AudioClip OnGetOut;

	public AudioClip EndGetIn;

	public AudioClip EndGetOut;

	private AudioSource source;

	private RCC_CarControllerV3 behavior;

	private EntityBehavior carBehavior;

	[SerializeField]
	private int gearCount = 4;

	private int cGear;

	private bool lastEngineStand;

	private void Start()
	{
		if (soundEngine == null)
		{
			return;
		}
		behavior = GetComponent<RCC_CarControllerV3>();
		if (behavior == null)
		{
			return;
		}
		source = new GameObject().AddComponent<AudioSource>();
		source.gameObject.transform.SetParent(base.transform);
		source.gameObject.transform.localPosition = Vector3.zero;
		if ((bool)BackgroundSoundManager.instance)
		{
			source.outputAudioMixerGroup = BackgroundSoundManager.instance.carMixerGroup;
		}
		source.clip = soundEngine;
		source.volume = VolumeController.VolumeSound;
		source.loop = true;
		source.pitch = 0f;
		source.spatialBlend = 1f;
		carBehavior = GetComponent<EntityBehavior>();
		carBehavior.AddEventListener(this, "OnGetIn", delegate
		{
			if (VolumeController.IsSoundEnable && OnGetIn != null)
			{
				AudioSource.PlayClipAtPoint(OnGetIn, base.transform.position, VolumeController.VolumeSound);
			}
		});
		carBehavior.AddEventListener(this, "OnGetOut", delegate
		{
			if (VolumeController.IsSoundEnable && OnGetOut != null)
			{
				AudioSource.PlayClipAtPoint(OnGetOut, base.transform.position, VolumeController.VolumeSound);
			}
		});
		carBehavior.AddEventListener(this, "EndGetIn", delegate
		{
			if (VolumeController.IsSoundEnable && EndGetIn != null)
			{
				AudioSource.PlayClipAtPoint(EndGetIn, base.transform.position, VolumeController.VolumeSound);
			}
			if (!source.isPlaying)
			{
				source.pitch = 0.7f;
				source.Play();
			}
		});
		carBehavior.AddEventListener(this, "EndGetOut", delegate
		{
			if (VolumeController.IsSoundEnable && EndGetOut != null)
			{
				AudioSource.PlayClipAtPoint(EndGetOut, base.transform.position, VolumeController.VolumeSound);
			}
			Stop();
		});
	}

	public void Stop()
	{
		if (source.isPlaying)
		{
			source.pitch = 0.7f;
			source.Stop();
		}
	}

	private void Update()
	{
		if (soundEngine == null || !behavior.engineRunning)
		{
			return;
		}
		if (source.clip == soundEngine)
		{
			source.volume = VolumeController.VolumeSound;
			if (behavior.engineRunning)
			{
				float num = 1f;
				if (behavior.gasInput != 0f)
				{
					num = 2f * (behavior.speed / (float)cGear) / (behavior.maxspeed / (float)gearCount);
				}
				float b = Mathf.Clamp(num + 0.5f * behavior.engineRPM / behavior.maxEngineRPM, 1f, 3f);
				source.pitch = Mathf.Lerp(source.pitch, b, Time.deltaTime * 5f);
			}
		}
		int num2 = (int)((behavior.speed - 1f) / behavior.maxspeed * (float)gearCount) + 1;
		if (source.isPlaying && num2 != cGear && behavior.gasInput != 0f && behavior.engineRunning && VolumeController.IsSoundEnable && num2 != 1 && soundGearChange != null)
		{
			AudioSource.PlayClipAtPoint(soundGearChange, base.transform.position, VolumeController.VolumeSound);
		}
		cGear = num2;
	}
}
